package com.example.administrator.wallet.view.activity.asset;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.MainBean;
import com.example.administrator.wallet.model.bean.TransactionDetailsBean;
import com.example.administrator.wallet.utils.DimbgUtils;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.mine.Contacts.AddContactActivity;
import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static com.loopj.android.http.AsyncHttpClient.log;

public class TransactionDetailsActivity extends Activity{


    private ImageButton finishImg;
    private TextView viewTitle;
    private TextView tv_value_dec;
    private TextView tv_coin_unit;
    private TextView date;
    private TextView tv_TranstionFee;
    private TextView tv_input;
    private TextView tv_confirmation_number;
    private TextView tv_output;
    private TextView tv_input_data;


    private RelativeLayout rl_transaction_id;
    private TextView tv_transaction_id;
    private RelativeLayout rl_address_of_transaction_destination;
    private TextView tv_address_of_transaction_destination;
    private  RelativeLayout rl_original_address_of_transaction;
    private TextView tv_original_address_of_transaction;


    private IRequestManager reqequestManager = RequestFactory.getReqequestManager();
    private TransactionDetailsBean bean;

    private View view;
    private RelativeLayout activity_transaction_details;
    private MainBean.DataBean.ListBean datas;

    private Button bt_hexadecimal;
    private Button bt_string;
    private RelativeLayout rl_hexadecimal,rl_string;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction_details);

        setStatuColor();

        finishImg = (ImageButton) findViewById(R.id.finish_img);
        viewTitle = (TextView) findViewById(R.id.view_title);
        tv_value_dec = (TextView) findViewById(R.id.tv_value_dec);
        tv_coin_unit = (TextView) findViewById(R.id.tv_coin_unit);
        date = (TextView) findViewById(R.id.date);
        tv_TranstionFee = (TextView) findViewById(R.id.tv_TranstionFee);
        tv_input = (TextView) findViewById(R.id.tv_input);
        tv_confirmation_number = (TextView) findViewById(R.id.tv_confirmation_number);
        tv_output = (TextView) findViewById(R.id.tv_output);
        tv_input_data = (TextView) findViewById(R.id.tv_input_data);

        bt_hexadecimal = (Button) findViewById(R.id.bt2_hexadecimal);
        bt_string = (Button) findViewById(R.id.bt1_string);
        rl_hexadecimal = (RelativeLayout) findViewById(R.id.rl_hexadecimal);
        rl_string = (RelativeLayout) findViewById(R.id.rl_string);

        rl_transaction_id = (RelativeLayout) findViewById(R.id.rl_transaction_id);
        tv_transaction_id = (TextView) findViewById(R.id.tv_transaction_id);

        rl_address_of_transaction_destination = (RelativeLayout) findViewById(R.id.rl_address_of_transaction_destination);
        tv_address_of_transaction_destination = (TextView) findViewById(R.id.tv_address_of_transaction_destination);

        rl_original_address_of_transaction = (RelativeLayout) findViewById(R.id.rl_original_address_of_transaction);
        tv_original_address_of_transaction = (TextView) findViewById(R.id.tv_original_address_of_transaction);

        activity_transaction_details = (RelativeLayout) findViewById(R.id.activity_transaction_details);

        String tx_hash = getIntent().getStringExtra("tx_hash");
        log.e("tag!!!",tx_hash);
        String coin_symbol = getIntent().getStringExtra("coin_symbol");
        log.e("tag!!!",coin_symbol);
        datas = RecordActivity.addr;
        Log.e("tag!!!",datas.getAddr());

        finishImg.setVisibility(View.VISIBLE);
        String data4 = getString(R.string.transaction_details).toString();
        viewTitle.setText(coin_symbol + data4);

        getWindow().setStatusBarColor(getResources().getColor(R.color.statu));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);


        reqequestManager.httpPost(Api.BaseUrl + Api.Transaction_Detail, Api.setTransactionDetail(SharedPrefsUtil.getValue(TransactionDetailsActivity.this,"wallet", "token",""),datas.getAddr(),tx_hash,coin_symbol), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                Gson gson = new Gson();
                bean = gson.fromJson(Response, TransactionDetailsBean.class);
                if (bean.getCode() == 200) {
                    tv_value_dec.setText(bean.getData().getValue_dec());
                    tv_coin_unit.setText(bean.getData().getCoin_unit());
                    date.setText(bean.getData().getDate());
                    tv_input.setText(bean.getData().getInput());
                    int c = bean.getData().getConfirmation_number();
                    tv_confirmation_number.setText(String.valueOf(c));
                    tv_output.setText(bean.getData().getOutput());
                    tv_input_data.setText(bean.getData().getInput_data());
                    tv_transaction_id.setText(bean.getData().getTx_hash());
                    tv_address_of_transaction_destination.setText(bean.getData().getTo());
                    tv_original_address_of_transaction.setText(bean.getData().getFrom());
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("TAG", error);
            }
        });

        bt_hexadecimal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int value = 1;
                //显示十六进制
                rl_hexadecimal.setVisibility(View.VISIBLE );
                rl_string.setVisibility(View.GONE);
//                rl_hexadecimal.setVisibility(View.GONE);
//                rl_string.setVisibility(View.VISIBLE);

            }
        });

        bt_string.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int value = 0;
                //显示字符
                rl_hexadecimal.setVisibility(View.GONE);
                rl_string.setVisibility(View.VISIBLE);
//                rl_hexadecimal.setVisibility(View.VISIBLE );
//                rl_string.setVisibility(View.GONE);
            }
        });

        rl_transaction_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String addr = bean.getData().getTo();
                initCurrency(tv_transaction_id,addr);
            }
        });
        rl_address_of_transaction_destination.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String addr = bean.getData().getTx_hash();
                initCurrency(tv_address_of_transaction_destination,addr);
            }
        });
        rl_original_address_of_transaction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String addr = bean.getData().getFrom();
                initCurrency(tv_original_address_of_transaction,addr);
            }
        });
        finishImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);
    }

    private void initCurrency(final TextView ed,final String addr) {
        view = View.inflate(this, R.layout.popup_addr, null);
        final Button bt_copy = (Button) view.findViewById(R.id.open_camera);
        final Button bt_add_contact = (Button) view.findViewById(R.id.open_photo);
        final Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);



        new Handler().post(new Runnable() {
            @Override
            public void run() {
                final PopupWindow window = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT, true);
                window.setFocusable(true);
                window.setTouchable(true);
                window.setOutsideTouchable(true);
                window.showAtLocation(activity_transaction_details, Gravity.CENTER, 0, 0);
                Log.e("TAG-----", "OK");
                DimbgUtils.dimBackground(TransactionDetailsActivity.this, 0.5f);
                window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        DimbgUtils.dimBackground(TransactionDetailsActivity.this, 1.0f);
                    }
                });

                bt_copy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
                        CharSequence charSequence = ed.getText().toString();
                        ClipData clip = ClipData.newPlainText("label",charSequence);
                        clipboard .setPrimaryClip(clip);
                        Toast.makeText(getApplicationContext(), R.string.copy_success, Toast.LENGTH_SHORT).show();
                        window.dismiss();

                    }
                });
                bt_add_contact.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
                        CharSequence charSequence = ed.getText().toString();
                        ClipData clip = ClipData.newPlainText("label",charSequence);
                        clipboard .setPrimaryClip(clip);
                        Intent i = new Intent();
                        i.putExtra("addr",addr);
                        startActivity(new Intent(getApplicationContext(), AddContactActivity.class));
                        window.dismiss();
                        overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);
                    }
                });
                bt_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                    }
                });

            }
        });

    }
    public void setStatuColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(android.R.color.white));
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setMiuiStatusBarDarkMode(this, true);
        setMiuiStatusBarDarkMode(this, true);
    }

    //设置成白色的背景，字体颜色为黑色。
    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }

}
